package com.usian.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import com.usian.mapper.*;
import com.usian.pojo.*;
import com.usian.redis.RedisClient;
import com.usian.utils.IDUtils;
import com.usian.utils.PageResult;
import com.usian.utils.Result;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    private TbItemMapper tbItemMapper;
    @Autowired
    private TbItemCatMapper tbItemCatMapper;
    @Autowired
    private TbItemDescMapper tbItemDescMapper;
    @Autowired
    private TbItemParamMapper tbItemParamMapper;
    @Autowired
    private TbItemParamItemMapper tbItemParamItemMapper;
    @Autowired
    private RedisClient redisClient;
    @Value("${ITEM_INFO}")
    private String ITEM_INFO;

    @Value("${BASE}")
    private String BASE;

    @Value("${DESC}")
    private String DESC;
    @Value("${PARAM}")
    private String PARAM;

    @Value("${ITEM_INFO_EXPIRE}")
    private Integer ITEM_INFO_EXPIRE;


    @Override
    public List<TbItem> findall() {
        return tbItemMapper.selectByExample(new TbItemExample());
    }

    @Override
    public PageResult selectTbItemAllByPage(Integer page, Integer rows) {
        PageHelper.startPage(page, rows);
        TbItemExample t = new TbItemExample();
        TbItemExample.Criteria criteria = t.createCriteria();
        criteria.andStatusEqualTo((byte) 1);
        List<TbItem> list = tbItemMapper.selectByExample(t);
        PageInfo<TbItem> pageInfo = new PageInfo<>(list);
        return new PageResult(pageInfo.getPageNum(), pageInfo.getPages(), pageInfo.getList());
    }

    @Override
    public List<TbItemCat> selectItemCategoryByParentId(Integer id) {
        TbItemCatExample tbItemCatExample = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = tbItemCatExample.createCriteria();
        criteria.andParentIdEqualTo((long) id);
        List<TbItemCat> list = tbItemCatMapper.selectByExample(tbItemCatExample);

        return list;
    }

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Override
    public int insertTbItem(TbItem tbItem, String desc, String itemParams) {
        Date date = new Date();
        long l = IDUtils.genItemId();
        tbItem.setId(l);
        tbItem.setCreated(date);
        tbItem.setUpdated(date);
        tbItem.setStatus((byte) 1);
        tbItem.setPrice(tbItem.getPrice() * 100);
        int i = tbItemMapper.insertSelective(tbItem);


        TbItemDesc tbItemDesc = new TbItemDesc();
        tbItemDesc.setCreated(date);
        tbItemDesc.setUpdated(date);
        tbItemDesc.setItemDesc(desc);
        tbItemDesc.setItemId(l);
        int i1 = tbItemDescMapper.insertSelective(tbItemDesc);

        TbItemParamItem tbItemParam = new TbItemParamItem();
        tbItemParam.setItemId(tbItem.getId());
        tbItemParam.setParamData(itemParams);
        tbItemParam.setCreated(date);
        tbItemParam.setUpdated(date);
        int i2 = tbItemParamItemMapper.insertSelective(tbItemParam);


        amqpTemplate.convertAndSend("item_exchage", "item.add", tbItem.getId());


        return i + i1 + i2;
    }

    @Override
    public int deleteItemById(String itemId) {
        TbItem tbItem = new TbItem();
        tbItem.setId(Long.parseLong(itemId));
        tbItem.setStatus((byte) 3);
        return tbItemMapper.updateByPrimaryKeySelective(tbItem);
    }

    @Override
    public Map<String, Object> preUpdateItem(Long itemId) {
        Map<String, Object> map = new HashMap<>();
        TbItem tbItem = tbItemMapper.selectByPrimaryKey(itemId);
        map.put("item", tbItem);
        TbItemCat tbItemCat = tbItemCatMapper.selectByPrimaryKey(tbItem.getCid());
        map.put("itemCat", tbItemCat.getName());
        TbItemDesc tbItemDesc = tbItemDescMapper.selectByPrimaryKey(itemId);
        map.put("itemDesc", tbItemDesc.getItemDesc());


        TbItemParamItemExample tbItemParamExample = new TbItemParamItemExample();

        TbItemParamItemExample.Criteria criteria = tbItemParamExample.createCriteria();
        criteria.andItemIdEqualTo(itemId);
        List<TbItemParamItem> list = tbItemParamItemMapper.selectByExampleWithBLOBs(tbItemParamExample);
        if (list.size() > 0) {
            map.put("itemParamItem", list.get(0).getParamData());
        }


        return map;
    }

    @Override
    public int updateTbItem(TbItem tbItem, String desc, String itemParams) {
        Date date = new Date();
        tbItem.setUpdated(date);
        tbItem.setStatus((byte) 1);
        int i = tbItemMapper.updateByPrimaryKeySelective(tbItem);

        TbItemDesc tbItemDesc = new TbItemDesc();
        tbItemDesc.setItemId(tbItem.getId());
        tbItemDesc.setItemDesc(desc);
        tbItemDesc.setUpdated(date);
        int i1 = tbItemDescMapper.updateByPrimaryKeySelective(tbItemDesc);


        TbItemParamItem tbItemParam = new TbItemParamItem();
        TbItemParamItemExample t = new TbItemParamItemExample();
        TbItemParamItemExample.Criteria criteria = t.createCriteria();
        criteria.andItemIdEqualTo(tbItem.getId());

        List<TbItemParamItem> list = tbItemParamItemMapper.selectByExampleWithBLOBs(t);
        if (list.size() > 0) {
            tbItemParam.setId(list.get(0).getId());
        }
        tbItemParam.setItemId(tbItem.getId());
        tbItemParam.setParamData(itemParams);
        tbItemParam.setUpdated(date);
        int i2 = tbItemParamItemMapper.updateByPrimaryKeySelective(tbItemParam);


        return i + i1 + i2;
    }

    @Override
    public TbItem selectItemInfo(long itemId) {
        //查询缓存
        TbItem tbItem = (TbItem) redisClient.get(ITEM_INFO + ":" + itemId + ":" + BASE);
        if (tbItem != null) {
            return tbItem;
        }
        tbItem = tbItemMapper.selectByPrimaryKey(itemId);
        redisClient.set(ITEM_INFO + ":" + itemId + ":" + BASE, tbItem);
        redisClient.expire(ITEM_INFO + ":" + itemId + ":" + BASE, ITEM_INFO_EXPIRE);
        return tbItem;
    }

    @Override
    public TbItemDesc selectItemDescByItemId(long itemId) {
        TbItemDesc tbItemDesc = (TbItemDesc) redisClient.get(
                ITEM_INFO + ":" + itemId + ":" + DESC);
        if (tbItemDesc != null) {
            return tbItemDesc;
        }
        TbItemDescExample tbItemDescExample = new TbItemDescExample();
        TbItemDescExample.Criteria criteria = tbItemDescExample.createCriteria();
        criteria.andItemIdEqualTo(itemId);
        List<TbItemDesc> list = tbItemDescMapper.selectByExampleWithBLOBs(tbItemDescExample);
        if (list != null && list.size() > 0) {
            redisClient.set(ITEM_INFO + ":" + itemId + ":"+ DESC,list.get(0));
            redisClient.expire(ITEM_INFO + ":" + itemId + ":"+ DESC,ITEM_INFO_EXPIRE);

            return list.get(0);
        }
        return null;
    }

    @Override
    public TbItemParamItem selectTbItemParamItemByItemId(long itemId) {
         TbItemParamItem tbItemParamItem=(TbItemParamItem)redisClient.get(ITEM_INFO + ":" + itemId + ":" +
                PARAM);
         if (tbItemParamItem!=null){
             return tbItemParamItem;
         }

        TbItemParamItemExample tbItemParamItemExample = new TbItemParamItemExample();
        TbItemParamItemExample.Criteria criteria = tbItemParamItemExample.createCriteria();
        criteria.andItemIdEqualTo(itemId);
        List<TbItemParamItem> list = tbItemParamItemMapper.selectByExampleWithBLOBs(tbItemParamItemExample);
        if (list!=null &&list.size()>0){
            redisClient.set(ITEM_INFO + ":" + itemId + ":"+
                    PARAM,list.get(0));
            redisClient.expire(ITEM_INFO + ":" + itemId + ":"+
                    PARAM,ITEM_INFO_EXPIRE);
            return list.get(0);
        }

        return null;
    }
}
